package com.ken.student.controller;

import com.ken.common.dto.signInDto;
import com.ken.common.pojo.answer;
import com.ken.common.pojo.course;
import com.ken.common.pojo.studyRecord;
import com.ken.common.pojo.wrong_question;
import com.ken.common.result.Result;
import com.ken.student.dto.answerDto;
import com.ken.student.dto.is_postDto;
import com.ken.student.dto.post_answerDto;
import com.ken.student.dto.submit_homeworkDto;
import com.ken.student.pojo.TF;
import com.ken.student.pojo.knowledge_statistics;
import com.ken.student.service.studentService;
import com.ken.student.vo.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
@Slf4j
@RestController
@RequestMapping("/student")
public class studentController {
    @Autowired
    private studentService studentService;

    /**
     * 学生签到接口
     * @param dto 内涵学生id和课程id
     * @return 返回签到信息
     * @throws IOException
     */
    @PostMapping("/signIn")
    public Result<String> signIn(@RequestBody signInDto dto) throws IOException {
        return studentService.signIn(dto);
    }

    /**
     * 根据学号获取与作业的绑定信息
     * @param student_id 学生学号
     * @return 返回绑定信息的数组
     */
    @PostMapping("/getHomeworkById")
    public Result<List<homeworkVo>> getHomeworkById(String student_id){
        List<homeworkVo> homeworks = studentService.getHomeworkById(student_id);
        return Result.success(homeworks);
    }

    /**
     * 根据学号获取学生的考试情况
     * @param student_id 学生学号
     * @return 返回考试情况信息
     */
    @PostMapping("/getExamById")
    public Result<List<examVo>> getExamById(String student_id){
        List<examVo> exams = studentService.getExamById(student_id);
        return Result.success(exams);
    }

    /**
     * 学生回答问题接口
     * @param answerList answer_id不用提交
     * @return 返回回答信息
     */
    @PostMapping("/post_answer")
    public Result<TF> post_answer(@RequestBody post_answerDto answerList){
        log.info(answerList.toString());
        //ArrayList<answer> answers = new ArrayList<>();
        TF tf = new TF();
        for(answer answer:answerList.getAnswerList()){
            if (studentService.is_post(answer)){
                return Result.error("已提交");
            }
            if (studentService.post_answer(answer).getIs_corrected().equals("错误")){
                tf.setFalse_num(tf.getFalse_num()+1);
            }else {
                tf.setCorrect_num(tf.getCorrect_num()+1);
            }
        }
        return Result.success(tf);
    }

    /**
     * 判断是否提交接口
     * @param dto 题目的id
     * @return 若未提交返回null若提交则返回学生回答的集合
     */
    @PostMapping("/is_post")
    public Result<List<answer>> is_post(@RequestBody is_postDto dto){
        List<answer> answers = studentService.is_post2(dto.getQuestionList());
        return Result.success(answers);
    }
//    /**
//     * 根据学生id获取学习记录
//     * @param student_id 学生id
//     * @return 学习记录的数组
//     */
//    public Result<List<studyRecord>> getRecordByStudentId(String student_id){
//        return null;
//    }

    /**
     * 获取学生的消息信息
     * @param student_id 学生的学号
     * @return 返回消息内容列表
     */
    @GetMapping("/getStudentMessage")
    public Result<List<messageVo>> getStudentMessage(@Param("student_id") String student_id){
        List<messageVo> studentMessage = studentService.getStudentMessage(student_id);
        return Result.success(studentMessage);
    }

    /**
     * 根据学号获取课程和所属班级信息
     * @param student_id 学号
     * @return 信息详情
     */
    @GetMapping("/getCourseByStudentId")
    public Result<List<courseVo>> getCourseByStudentId(@Param("student_id") String student_id){
        List<courseVo> courseByStudentId = studentService.getCourseByStudentId(student_id);
        return Result.success(courseByStudentId);
    }

    /**
     * 提交作业
     * @param dto 提交作业所需的数据
     * @return 返回提交情况
     */
    @PostMapping("/submit_homework")
    public Result<String> submit_homework(@RequestBody submit_homeworkDto dto){
        studentService.submit_homework(dto);
        return Result.success("提交成功");
    }

    /**
     * 获取回答信息
     * @param student_id 学生id
     * @param question_id 问题id
     * @return 返回回答情况
     */
    @GetMapping("/getAnswerById")
    public Result<answer> getAnswerById(@RequestParam("student_id") String student_id,@RequestParam("question_id") String question_id){
        answer answer = studentService.getAnswerById(student_id, question_id);
        return Result.success(answer);
    }

    /**
     * 获取课时学习情况
     * @param lesson_id 课时id
     * @param student_id 学生id
     * @return 返回课时学习情况
     */
    @GetMapping("/getLessonRecord")
    public Result<List<studyRecord>> getLessonRecord(@RequestParam("lesson_id") String lesson_id,@RequestParam("student_id") String student_id){
        log.info("查询学生:"+student_id+"课程:"+lesson_id);
        List<studyRecord> records = studentService.getLessonRecord(lesson_id, student_id);
        return Result.success(records);
    }

    /**
     * 获取课时的问题作答情况
     * @param student_id 学生id
     * @param lesson_id 课时id
     * @param knowledgePoint_id 课时的知识点id
     * @return
     */
    @GetMapping("/getLessonQuestions")
    public Result<List<knowledge_statistics>> getLessonQuestions(@RequestParam("student_id") String student_id,@RequestParam("lesson_id") String lesson_id,@RequestParam("knowledgePoint_id") String knowledgePoint_id){
        List<knowledge_statistics> knowledgeStatistics = studentService.getLessonQuestions(student_id, lesson_id, knowledgePoint_id);
        return Result.success(knowledgeStatistics);
    }

    /**
     * 判断作业是否提交
     * @param student_id 学生id
     * @param homework_id 作业id
     * @return 返回提交情况
     */
    @GetMapping("/homework_isPost")
    public Result<String> homework_isPost(@RequestParam("student_id") String student_id,@RequestParam("homework_id") String homework_id){
        if (studentService.homework_isPost(student_id,homework_id)){
            return Result.success("已提交");
        }
        return Result.error("未提交");
    }

    /**
     * 获取课程的学习时间
     * @param course_id 课程id
     * @param student_id 学生id
     * @return 返回课程学习时间情况
     */
    @GetMapping("/getCourseStudyTime")
    public Result<studyTimeVo> getCourseStudyTime(@RequestParam("course_id") String course_id,@RequestParam("student_id") String student_id){
        studyTimeVo studyTime = studentService.getCourseStudyTime(course_id, student_id);
        return Result.success(studyTime);
    }

    /**
     * 保存错题
     * @param wrong_question 错题
     * @return 返回保存的错题信息
     */
    @PostMapping("/save_wrongQuestion")
    public Result<List<wrong_question>> save_wrongQuestion(@RequestBody List<wrong_question> wrong_question){
        List<wrong_question> wrongQuestions = studentService.save_wrongQuestion(wrong_question);
        return Result.success(wrongQuestions);
    }

    /**
     * 获取学生错题
     * @param student_id 学生id
     * @return 返回错题情况
     */
    @GetMapping("/get_wrongQuestion")
    public Result<List<wrong_questionVo>> get_wrongQuestion(@RequestParam("student_id") String student_id){
        List<wrong_questionVo> wrongQuestionVos = studentService.get_wrongQuestion(student_id);
        return Result.success(wrongQuestionVos);
    }

    /**
     * 将错题改为已修正状态
     * @param wrong_QuestionId 错题id
     * @return 返回修改情况
     */
    @GetMapping("/change_wrongQuestion")
    public Result<String> change_wrongQuestion(@RequestParam("wrong_QuestionId") String wrong_QuestionId){
        return Result.success("修改成功");
    }

    /**
     * 删除错题
     * @param wrongQuestionId 错题id
     * @return 返回删除信息
     */
    @DeleteMapping("/delete_wrongQuestion")
    public Result<String> delete_wrongQuestion(@RequestParam("wrongQuestionId") String wrongQuestionId){
        studentService.delete_wrongQuestion(wrongQuestionId);
        return Result.success("删除成功");
    }
}
